package com.edroid.autorunner.um;

import android.net.ConnectivityManager;
import android.telephony.TelephonyManager;

import com.example.utils.KvTool;

public class UserInfo {
	//数据库字段
	public int index;

	public String sdk, mac, aid, sid, num;
	public int sdk_int;
	
	//运营商参数
	public int op; //为了生成phoneType和opName
	public String opName;
	public String imei, imsi, simSerial;

	//网络参数
	public int g;
	public int networkType;
	public String netExtra;
	public int simNetworkType;
	public int phoneType;
	public String BSSID, SSID;
	/**
	 * 国家代码
	 */
	public String iso;
	public String model, board, brand, device, baseband, hardware, manufacturer, fingerprint;
	public int screenWidth, screenHeight, densityDpi;
	
	
	public void save(KvTool kv) {
		kv.putInt("user_g", g);
		kv.putInt("user_netType", networkType);
		kv.putInt("user_networkType", simNetworkType);
		kv.putInt("user_phoneType", phoneType);
		kv.putString("user_netExtra", netExtra);
		kv.putInt("user_op", op);
		kv.putString("user_opName", opName);
		kv.putString("user_num", num);
		kv.putString("user_iso", iso);
		
		kv.putString("user_imei", imei);
		kv.putString("user_imsi", imsi);
		kv.putString("user_sim", simSerial);
		kv.putString("user_sdk", sdk);
		kv.putInt("user_sdk_int", sdk_int);
		kv.putString("user_mac", mac);
		kv.putString("user_aid", aid);
		kv.putString("user_sid", sid);
		kv.putString("user_ssid", SSID);
		kv.putString("user_bssid", BSSID);
		
		kv.putString("user_model", model);
		kv.putString("user_manufacturer", manufacturer);
		kv.putString("user_hardware", hardware);
		kv.putString("user_device", device);
		kv.putString("user_board", board);
		kv.putString("user_brand", brand);
		kv.putString("user_baseband", baseband);
		kv.putString("user_fingerprint", fingerprint);
		
		kv.putInt("user_screenHeight", screenHeight);
		kv.putInt("user_screenWidth", screenWidth);
		kv.putInt("user_densityDpi", densityDpi);
	}
	
	public UserInfo read(KvTool kv, UserInfo def) {
		this.g = kv.getInt("user_g", def.g);
		this.networkType = kv.getInt("user_netType", def.networkType);
		this.simNetworkType = kv.getInt("user_networkType", def.simNetworkType);
		this.phoneType = kv.getInt("user_phoneType", def.phoneType);
		this.netExtra = kv.getString("user_netExtra", def.netExtra);
		this.op = kv.getInt("user_op", def.op);
		this.opName = kv.getString("user_opName", def.opName);
		this.num = kv.getString("user_num", def.num);
		this.iso = kv.getString("user_iso", def.iso);
		
		this.imei = kv.getString("user_imei", def.imei);
		this.imsi = kv.getString("user_imsi", def.imsi);
		this.simSerial = kv.getString("user_sim", def.simSerial);
		this.sdk = kv.getString("user_sdk", def.sdk);
		this.sdk_int = kv.getInt("user_sdk_int", def.sdk_int);
		this.mac = kv.getString("user_mac", def.mac);
		this.aid = kv.getString("user_aid", def.aid);
		this.sid = kv.getString("user_sid", def.sid);
		this.SSID = kv.getString("user_ssid", def.SSID);
		this.BSSID = kv.getString("user_bssid", def.BSSID);
		
		this.model = kv.getString("user_model", def.model);
		this.board = kv.getString("user_board", def.board);
		this.brand = kv.getString("user_brand", def.brand);
		this.device = kv.getString("user_device", def.device);
		this.manufacturer = kv.getString("user_manufacturer", def.manufacturer);
		this.hardware = kv.getString("user_hardware", def.hardware);
		this.baseband = kv.getString("user_baseband", def.baseband);
		this.fingerprint = kv.getString("user_fingerprint", def.fingerprint);
		
		this.screenHeight = kv.getInt("user_screenHeight", def.screenHeight);
		this.screenWidth = kv.getInt("user_screenWidth", def.screenWidth);
		this.densityDpi = kv.getInt("user_densityDpi", def.densityDpi);
		
		return this;
	}
	
	public String getLine1Number() {
		return num;
	}
	
	public String getNetworkOperator() {
		return getSimOperator();
	}
	
	public String getNetworkOperatorName() {
		return opName;
	}
	
	public String getNetworkCountryIso() {
		return iso;
	}
	
	/**
     * @see #PHONE_TYPE_NONE
     * @see #PHONE_TYPE_GSM
     * @see #PHONE_TYPE_CDMA
     * @see #PHONE_TYPE_SIP
     */
    public int getPhoneType() {
        return phoneType;
    }
    
    /**
     * @see #NETWORK_TYPE_UNKNOWN
     * 
     * @see #NETWORK_TYPE_GPRS
     * @see #NETWORK_TYPE_EDGE
     * @see #NETWORK_TYPE_CDMA
     * @see #NETWORK_TYPE_1xRTT
     * 
     * @see #NETWORK_TYPE_UMTS
     * @see #NETWORK_TYPE_HSDPA
     * @see #NETWORK_TYPE_HSUPA
     * @see #NETWORK_TYPE_HSPA
     * @see #NETWORK_TYPE_EVDO_0
     * @see #NETWORK_TYPE_EVDO_A
     * @see #NETWORK_TYPE_EVDO_B
     * @see #NETWORK_TYPE_IDEN
     * @see #NETWORK_TYPE_LTE
     * @see #NETWORK_TYPE_EHRPD
     * @see #NETWORK_TYPE_HSPAP
     */
    public int getSimNetworkType() {
    	return simNetworkType;
    }
	
    public String getSimCountryIso() {
    	return iso;
    }
    
    public String getNetExtra() {
		return netExtra;
	}
    
    public boolean isWifi() {
		return networkType == ConnectivityManager.TYPE_WIFI;
	}
    
    /**
     * 网络制式名字
     * 
     * @param model
     * @return
     */
    public String getSimNetworkTypeName() {
        switch (simNetworkType) {
            case TelephonyManager.NETWORK_TYPE_GPRS:
                return "GPRS";
            case TelephonyManager.NETWORK_TYPE_EDGE:
                return "EDGE";
            case TelephonyManager.NETWORK_TYPE_UMTS:
                return "UMTS";
            case TelephonyManager.NETWORK_TYPE_HSDPA:
                return "HSDPA";
            case TelephonyManager.NETWORK_TYPE_HSUPA:
                return "HSUPA";
            case TelephonyManager.NETWORK_TYPE_HSPA:
                return "HSPA";
            case TelephonyManager.NETWORK_TYPE_CDMA:
                return "CDMA";
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
                return "CDMA - EvDo rev. 0";
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
                return "CDMA - EvDo rev. A";
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
                return "CDMA - EvDo rev. B";
            case TelephonyManager.NETWORK_TYPE_1xRTT:
                return "CDMA - 1xRTT";
            case TelephonyManager.NETWORK_TYPE_LTE:
                return "LTE";
            case TelephonyManager.NETWORK_TYPE_EHRPD:
                return "CDMA - eHRPD";
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return "iDEN";
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                return "HSPA+";
            default:
                return "UNKNOWN";
        }
    }
    
    public String getNetworkTypeName() {
        switch (networkType) {
            case ConnectivityManager.TYPE_MOBILE:
                return "MOBILE";
            case ConnectivityManager.TYPE_WIFI:
                return "WIFI";
            case ConnectivityManager.TYPE_MOBILE_MMS:
                return "MOBILE_MMS";
            case ConnectivityManager.TYPE_MOBILE_SUPL:
                return "MOBILE_SUPL";
            case ConnectivityManager.TYPE_MOBILE_DUN:
                return "MOBILE_DUN";
            case ConnectivityManager.TYPE_MOBILE_HIPRI:
                return "MOBILE_HIPRI";
            case ConnectivityManager.TYPE_WIMAX:
                return "WIMAX";
            case ConnectivityManager.TYPE_BLUETOOTH:
                return "BLUETOOTH";
            case ConnectivityManager.TYPE_DUMMY:
                return "DUMMY";
            case ConnectivityManager.TYPE_ETHERNET:
                return "ETHERNET";
            case ConnectivityManager.TYPE_MOBILE_FOTA:
                return "MOBILE_FOTA";
            case ConnectivityManager.TYPE_MOBILE_IMS:
                return "MOBILE_IMS";
            case ConnectivityManager.TYPE_MOBILE_CBS:
                return "MOBILE_CBS";
            case ConnectivityManager.TYPE_WIFI_P2P:
                return "WIFI_P2P";
                
            default:
                return Integer.toString(networkType);
        }
    }
    
    /**
     * 手机上网的联网类型
     * 
     * @return
     */
    public int getNetworkType() {
		return networkType;
	}
    
	public String getSimOperator() {
		if(imsi == null)
			return "46001";
		return imsi.substring(0, 5);
	}
	
	public String getSimOperatorName() {
		return opName;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(128);
		
		sb.append("{imei=").append(imei).append(',')
			.append("imsi=").append(imsi).append(',')
			.append("sim=").append(simSerial).append(',')
			.append("opName=").append(this.opName).append(',')
			.append("phoneType=").append(phoneType).append(',')
			.append("networkType=").append(simNetworkType).append(',')
			.append("netExtra=").append(netExtra).append(',')
			.append("iso=").append(this.iso).append(',')
			.append("num=").append(num).append(',')
			.append("mac=").append(mac).append(',')
			.append("model=").append(model).append(',')
			.append("manufacturer=").append(manufacturer).append(',')
			.append("hardware=").append(hardware).append(',')
			.append("aid=").append(aid).append(',')
			.append("sid=").append(sid).append(',')
			.append("SSID=").append(SSID).append(',')
			.append("BSSID=").append(BSSID).append(',')
			.append("Resolution=").append(screenWidth).append(' ').append(screenHeight).append(' ').append(densityDpi)
			.append('}');
		
		return sb.toString();
	}
}
